package insight.miescor.opp.domain;

import java.util.Date;
import java.util.List;

import org.json.JSONException;
import org.json.JSONObject;
import org.sormula.Database;
import org.sormula.SormulaException;
import org.sormula.Table;
import org.sormula.annotation.Transient;

public class Assignment implements Cloneable {

	private int id;
	private String opportunityId;
	private String role;
	private int projectObjId;
	private String userId;
	private String status;
	private String approvalType;
	private Date startDate;
	private Date endDate;
	private int reviewId;
	private int stepId;
	private String outcome;

	@Transient
	private Opportunity opportunity;

	public static int generateId(Database database) throws SormulaException {
		NumberGenerator numGen = NumberGenerator.loadNextNum(database,
				"AssignmentId");

		return numGen.getSeriesCurrentNum();

	}

	public Assignment() {
	}

	public Assignment(String role, String userId, String appType, int reviewId,
			int stepId, Opportunity oppObj, Database database)
			throws SormulaException {
		this.id = generateId(database);
		this.opportunityId = oppObj.getId();
		this.role = role;
		this.projectObjId = oppObj.getProjectId();
		this.userId = userId;
		this.approvalType = appType;
		this.reviewId = reviewId;
		this.stepId = stepId;
		this.opportunity = oppObj;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getOpportunityId() {
		return opportunityId;
	}

	public void setOpportunityId(String opportunityId) {
		this.opportunityId = opportunityId;

	}

	public String getRole() {
		return role;
	}

	public void setRole(String role) {
		this.role = role;
	}

	public int getProjectObjId() {
		return projectObjId;
	}

	public void setProjectObjId(int projectObjId) {
		this.projectObjId = projectObjId;
	}

	public String getUserId() {
		return userId;
	}

	public void setUserId(String userId) {
		this.userId = userId;
	}

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	public String getApprovalType() {
		return approvalType;
	}

	public void setApprovalType(String approvalType) {
		this.approvalType = approvalType;
	}

	public Date getStartDate() {
		return startDate;
	}

	public void setStartDate(Date startDate) {
		this.startDate = startDate;
	}

	public Date getEndDate() {
		return endDate;
	}

	public void setEndDate(Date endDate) {
		this.endDate = endDate;
	}

	public int getReviewId() {
		return reviewId;
	}

	public void setReviewId(int reviewId) {
		this.reviewId = reviewId;
	}

	public int getStepId() {
		return stepId;
	}

	public void setStepId(int stepId) {
		this.stepId = stepId;
	}

	public Opportunity getOpportunity() {
		return opportunity;
	}

	public void setOpportunity(Opportunity opportunity) {
		this.opportunity = opportunity;
	}

	public String getOutcome() {
		return outcome;
	}

	public void setOutcome(String outcome) {
		this.outcome = outcome;
	}

	public JSONObject getJson() throws JSONException {
		JSONObject jObj = new JSONObject().put("id", this.getId())
				.put("opportunityId", this.getOpportunityId())
				.put("startDate", this.getStartDate())
				.put("endDate", this.getEndDate())
				.put("status", this.getStatus())
				.put("stepId", this.getStepId()).put("role", this.getRole())
				.put("title", this.getOpportunity().getTitle())
				.put("type", this.getOpportunity().getProjectType());

		return jObj;
	}

	@Override
	public Object clone() throws CloneNotSupportedException {
		return super.clone();
	}

	public void insertAssignment(Database database) throws SormulaException {
		Table<Assignment> assTable = database.getTable(Assignment.class);
		assTable.insert(this);
	}

	public static Assignment getAssignmentById(Database database, String itemId)
			throws SormulaException {
		Table<Assignment> assTable = database.getTable(Assignment.class);
		Assignment ass = assTable.select(itemId);
		ass.setOpportunity(Opportunity.getOpportunityById(database,
				ass.getOpportunityId()));
		return ass;
	}
	
	public static int getStepId(Database database, String itemId)
			throws SormulaException {
		Table<Assignment> assTable = database.getTable(Assignment.class);
		Assignment ass = assTable.select(itemId);
		return ass.getStepId();
	}
	
	public static List<Assignment> getAssignmentDetailByOppId(Database database, String opportunityId)
			throws SormulaException {
		Table<Assignment> assTable = database.getTable(Assignment.class);		
		List<Assignment> assList = assTable.selectAllCustom("where opportunityId = ?", opportunityId);
		//List<Assignment> assList = assTable.selectAllWhereOrdered("opportunityId = ?","StartDate", opportunityId);
		return assList;
	}

	
	
	@Override
	public String toString() {
		return "Assignment [id=" + id + ", opportunityId=" + opportunityId
				+ ", role=" + role + ", projectObjId=" + projectObjId
				+ ", userId=" + userId + ", status=" + status
				+ ", approvalType=" + approvalType + ", startDate=" + startDate
				+ ", endDate=" + endDate + ", reviewId=" + reviewId
				+ ", stepId=" + stepId + ", outcome=" + outcome
				+ ", opportunity=" + opportunity + "]";
	}

	

}
